package com.wiley.service;

import java.net.MalformedURLException;

import org.openqa.selenium.By;

import com.wiley.publicMethod.BasePage;
import com.wiley.publicMethod.LoggerControler;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public class PersonalProfilePage extends BasePage {
	LoggerControler log;

	/**
	 * 页面元素
	 */
	// 我的
	public static final By mine_item_loc = By.id(BaseId + "mine_item");

	// 头像 avatar
	public static final By out_avatar_loc = By.id(BaseId + "avatar");
	
	// 头像（内）
	public static final By in_avatar_loc = By.name("头像");

	// 昵称
	public static final By nick_loc = By.name("昵称");

	// 输入框
	public static final By input_loc = By.id(BaseId + "input");

	// 完成
	public static final By finish_loc = By.id(BaseId + "button");

	// 签名
	public static final By signature_loc = By.name("签名");

	// 生日
	public static final By birthday_loc = By.name("生日");

	// 设定
	public static final By set_loc = By.id("android:id/button1");

	// 性别
	public static final By sex_loc = By.name("性别");

	// 男
	public static final By male_loc = By.name("男");

	// 女
	public static final By female_loc = By.name("女");

	// 家乡
	public static final By hometown_loc = By.name("家乡");

	// 我的地址管理
	public static final By addresss_loc = By.name("我的地址管理");

	// 个性化标签
	public static final By tags_loc = By.name("个性化标签");

	/**
	 * 操作方法
	 */
	// 点击我的
	public void clickMyItem() {
		this.clickEle(mine_item_loc);
	}

	// 点击我的头像
	public void clickMyAvatar() {
		this.clickEle(out_avatar_loc);
	}
	
	// 修改头像
	public void changeAvatar() {
		this.clickEle(in_avatar_loc);
		this.clickEle(By.name("从相册中选择上传"));
		TouchAction gesture = new TouchAction(driver).tap(200, 300).release();
		gesture.perform();
		this.clickEle(By.id("com.android.gallery:id/save"));
		/*this.clickEle(By.name("拍照上传"));
		//自拍
		this.clickEle(By.id("com.android.camera:id/v6_camera_picker"));
		try {
			Thread.sleep(3000);
			this.clickEle(By.id("com.android.camera:id/v6_shutter_button_internal"));
			this.clickEle(By.id("com.android.camera:id/v6_btn_done"));
			this.clickEle(By.id("com.miui.gallery:id/wallpaper_apply"));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}*/
	}

	// 修改昵称
	public void alterNickName(String name) {
		this.clickEle(nick_loc);
		this.sendKeys(input_loc, name);
		this.clickEle(finish_loc);
	}

	// 修改签名
	public void alterSignature(String signature) {
		this.clickEle(signature_loc);
		this.sendKeys(input_loc, signature);
		this.clickEle(finish_loc);
	}

	// 设定生日
	public void setBirthday() {
		this.clickEle(birthday_loc);
		this.clickEle(set_loc);
	}

	// 设定性别
	public void setSex() {
		this.clickEle(sex_loc);
		this.clickEle(male_loc);
	}

	// 设定家乡
	public void setHometown() {
		this.clickEle(hometown_loc);
		driver.scrollToExact("福建");
		this.clickEle(By.name("福建"));
		this.clickEle(By.name("宁德"));
	}

	// 地址管理
	public void manageAddress() {
		this.clickEle(addresss_loc);
	}

	// 设定个性化标签
	public void setTags() {
		this.clickEle(tags_loc);
		String[] args = {"特色料理", "主食", "提高免疫力" ,"上班族"};
		for(int i = 0; i < args.length; i++){
			this.clickEle(By.name(args[i]));
		}
		this.clickEle(finish_loc);
	}

	/**
	 * 默认登录状态下
	 * @throws MalformedURLException
	 */
	public void setPersonalProfile() throws MalformedURLException {
		this.clickMyItem();
		this.clickMyAvatar();
		this.changeAvatar();
		//this.alterNickName("wiley洛神");
		//this.alterSignature("wiley is the best!");
		this.setBirthday();
		//this.setSex();
		//this.setHometown();
		//this.setTags();
	}

	/**
	 * 业务封装
	 * 
	 * @throws MalformedURLException
	 * @throws InterruptedException
	 */
	public void personalProfileSet(Class name) throws MalformedURLException {
		log = this.MyLogger(name);
		AndroidDriver driver = super.SetDriver();
		this.setPersonalProfile();
		log.info("该case测试通过！");
	}
}
